package com.cq.jackmar.rxdemo.base;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cq.jackmar.rxdemo.view.customdialog.DialogMaker;

import butterknife.ButterKnife;


/**
 * @Title fragment 基类
 * @Author JackMar
 * @Date 2016-05-23 17:14
 */
public abstract class BaseFragment extends Fragment implements DialogMaker.DialogCallBack {
    public Activity context;
    protected Dialog dialog;

    protected View rootView = null;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(getLayoutId(), container, false);
            ButterKnife.bind(this, rootView);
            initView();
        }
        //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        initData();
        return rootView;
    }


    /**
     * 参数设置
     */
    protected abstract void initView();

    /**
     * 参数设置
     */
    protected abstract void initData();


    /**
     * 初始化layouy
     *
     * @return
     */
    protected abstract int getLayoutId();


    /**
     * 弹出对话框
     *
     * @author blue
     */
    public Dialog showAlertDialog(String title, String msg, String[] btns, boolean isCanCancelabel, final boolean isDismissAfterClickBtn, Object tag) {
        if (null == dialog || !dialog.isShowing()) {
            dialog = DialogMaker.showCommonAlertDialog(context, title, msg, btns, this, isCanCancelabel, isDismissAfterClickBtn, tag);
        }
        return dialog;
    }

    /**
     * 等待对话框
     *
     * @author blue
     */
    public Dialog showWaitDialog(String msg, boolean isCanCancelabel, Object tag) {
        if (null == dialog || !dialog.isShowing()) {
            dialog = DialogMaker.showCommenWaitDialog(context, msg, this, isCanCancelabel, tag);
        }
        return dialog;
    }

    /**
     * 关闭对话框
     *
     * @author blue
     */
    public void dismissDialog() {
        if (null != dialog && dialog.isShowing()) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    dialog.dismiss();
                }
            }, 2000);

        }
    }

    /**
     * 点击dialog
     *
     * @param dialog
     * @param position 点击Button的索引.
     * @param tag
     */
    @Override
    public void onButtonClicked(Dialog dialog, int position, Object tag) {
    }

    /**
     * 取消dialog
     *
     * @param dialog
     * @param tag
     */
    @Override
    public void onCancelDialog(Dialog dialog, Object tag) {
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(rootView);
    }
}
